package month.statistic;

import utility.time.ProcessTime;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author lit
 * @date on 2018/3/20.
 */
public class Main {
    public static void main(String[] args){

        Calendar calendar = Calendar.getInstance();
        /**
         * 1.获取包含三个统计时间的字符串数组，日期由小到大
         */
        String[] statTime = getMonthStatTime(calendar);
        if(args != null && args.length > 0){
            statTime = getMonthStatTime(args[0]);
        }
        /**
         * 2.处理用户画像数据表
         */
        ProcessUserPortrait pup = new ProcessUserPortrait();
        pup.processUserPortrait(statTime);
        /**
         * 3.处理月统计数据表
         */
        ProcessStatMonth psm = new ProcessStatMonth();
        psm.processStatMonth(statTime);
        /**
         * 4.处理区域画像数据表
         */
        ProcessDistPortrait pdp = new ProcessDistPortrait();
        pdp.processDistPortrait(statTime);
    }

    /**
     * 获取统计周期
     * @return 由三个月的一号为日期组成的字符串数组,eg:3月份跑该程序时，得到["2018-01-01 00:00:00","2018-02-01 00:00:00","2018-03-01 00:00:00"],实际统计的月份是第二个日期，即对2月份进行月统计
     */
    private static String[] getMonthStatTime(Calendar calendar){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        int year = calendar.get(Calendar.YEAR);
        //Calendar.MONTH默认值范围是0-11
        int month = calendar.get(Calendar.MONTH);

        calendar.set(year,month,1,0,0,0);

        String thirdStatTime = sdf.format(calendar.getTime());
        calendar.add(Calendar.MONTH,-1);
        String secondStatTime = sdf.format(calendar.getTime());
        calendar.add(Calendar.MONTH,-1);
        String  firstStatTime= sdf.format(calendar.getTime());

        String[] result = {firstStatTime,secondStatTime,thirdStatTime};
        return  result;
    }

    private static String[] getMonthStatTime(String time){
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sdf.parse(time));

            return getMonthStatTime(calendar);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
